//
//  NewFeatureView.swift
//  weibo
//
//  Created by RDDBoss3 on 2018/7/17.
//  Copyright © 2018年 Fly. All rights reserved.
//

import UIKit

/// 新特性
class NewFeatureView: UIView {

    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet weak var enterBtn: UIButton!
    
    @IBOutlet weak var pageControl: UIPageControl!
    
    @IBAction func enterClicked(_ sender: UIButton) {
        removeFromSuperview()
    }
    
    class func newFeatureView() -> NewFeatureView {
        
        let nib = UINib(nibName: "NewFeatureView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! NewFeatureView
        
        // 从 xib 加载的视图，默认是 600*600 的大小
        v.frame = UIScreen.main.bounds
        
        return v
        
    }
    
    override func awakeFromNib() {
        
        // 如果使用自动布局设置的界面，从 xib 加载默认是 600 * 600
        // 添加四个图像视图
        
        let count = 4
        
        let rect = UIScreen.main.bounds
        
        pageControl.numberOfPages = count
        
        for i in 0..<count {
            
            let imageName = "ad\(i + 1)"
            
            let imageView = UIImageView(image: UIImage(named: imageName))
            
            imageView.frame = rect.offsetBy(dx: (CGFloat(i) * rect.width), dy: 0)
            
            scrollView.addSubview(imageView)
        }
        
        // 设置scrollView的属性
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        
        scrollView.delegate = self
        
        // 隐藏按钮
        enterBtn.isHidden = true
        
    }
    
//    override init(frame: CGRect) {
//
//        super.init(frame: frame)
//
//        backgroundColor = UIColor.orange
//
//    }
    
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}

extension NewFeatureView: UIScrollViewDelegate {
    
    /// 滑动减速
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
     
        // 滚动到最后一屏，让视图删除
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        let count = scrollView.subviews.count
        
        // 判断是否是最后一页
        if page == count {
            
            removeFromSuperview()
            
        }
        
        // 如果是倒数第2页，显示按钮
        enterBtn.isHidden = (page != count - 1)
        
    }
    
    /// 滚动
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        // 一旦滚动隐藏按钮
        enterBtn.isHidden = true
        
        // 计算当前偏移量，取整是考虑到四舍五入 + 0.5
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        // 设置分页
        pageControl.currentPage = page
        
        // 分页控件的隐藏
        pageControl.isHidden = (page == scrollView.subviews.count)
    }
    
}
